package com.intelligent.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.pojo.CustomerManagement;
import com.intelligent.pojo.CustomerReconciliation;
import com.intelligent.service.CustomerManagementService;
import com.intelligent.service.CustomerReconciliationService;
import com.intelligent.utils.Result;
import com.intelligent.vo.ReconciliationPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/customer-reconciliation")
public class CustomerReconciliationController {

    @Autowired
    private CustomerReconciliationService customerReconciliationService;

    @Autowired
    private CustomerManagementService customerManagementService;

    //新建对账单

    @PostMapping("/create")
    public Result<CustomerReconciliation> create(@RequestBody CustomerReconciliation reconciliation) {
        try {
            CustomerReconciliation result = customerReconciliationService.createReconciliation(reconciliation);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    //查询（分页）
    @PostMapping("/page")
    public Result<Page<CustomerReconciliation>> pageList(@RequestBody ReconciliationPage pageRequest) {
        Page<CustomerReconciliation> page = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());

        Page<CustomerReconciliation> result = customerReconciliationService.pageReconciliation(
            page,
            pageRequest.getRecoCode(),
            pageRequest.getCustomerId(),
            pageRequest.getStatus(),
            pageRequest.getCreateDate()
        );

        // 关联客户信息，显示客户名称
        if (result.getRecords() != null) {
            for (CustomerReconciliation reconciliation : result.getRecords()) {
                if (reconciliation.getCustomerId() != null) {
                    CustomerManagement customer = customerManagementService.getById(reconciliation.getCustomerId());
                    if (customer != null) {
                        // 添加客户名称字段到对账单对象中
                        reconciliation.setCustomerName(customer.getCustomerName());
                    }
                }
            }
        }

        return Result.success(result);
    }


     // 对账单详情
    @GetMapping("/{id}")
    public Result<CustomerReconciliation> detail(@PathVariable Long id) {
        CustomerReconciliation reconciliation = customerReconciliationService.getById(id);
        if (reconciliation == null) {
            return Result.error("未找到对账单");
        }

        // 关联客户信息
        if (reconciliation.getCustomerId() != null) {
            CustomerManagement customer = customerManagementService.getById(reconciliation.getCustomerId());
            if (customer != null) {
                reconciliation.setCustomerName(customer.getCustomerName());
            }
        }

        return Result.success(reconciliation);
    }


    // 更新对账状态
    @PutMapping("/{id}/status")
    public Result<CustomerReconciliation> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        try {
            boolean updated = customerReconciliationService.updateReconciliationStatus(id, status);
            if (updated) {
                CustomerReconciliation reconciliation = customerReconciliationService.getById(id);
                return Result.success(reconciliation);
            } else {
                return Result.error("更新状态失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    // 获取客户列表
    @GetMapping("/customers")
    public Result<List<CustomerManagement>> getCustomerList() {
        try {
            List<CustomerManagement> customers = customerManagementService.list();
            return Result.success(customers);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    //计算对账周期内的金额
    @RequestMapping("amounts")
    public void amounts(@RequestBody CustomerReconciliation reconciliation){
        customerReconciliationService.amounts(reconciliation);
    }
}
